<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
<div style="width: 80%;margin: 25px 10%;" class="container">
    <div class="layui-form-item">
        <label class="layui-form-label">关联分区</label>
        <div class="layui-input-block">
            <table id="demo2" lay-filter="test2"></table>
        </div>
    </div>
    <form class="layui-form" id="form" action="">
        <input type="hidden" id="id" name="id" />
        <input type="hidden" name="ids" id="ids">
        <div class="layui-form-item">
            <label class="layui-form-label">定区名称</label>
            <div class="layui-input-block">
                <input type="text" name="mingcheng" id="name"  placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">负责人</label>
            <div class="layui-input-block">
                <select name="fuzerenId" id="fuzerenId" id="fuzerenId" lay-verify="required">
                    <option value="-1">请选择</option>
                </select>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>-->
    </form>
</div>
</body>
</html>
<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
</script>
<script>
    $.get(new Local().base_url+"/user/qpfzr",function(data) {
        $.each(data.data, function () {
            var opt = $("<option></option>").appendTo("#fuzerenId");
            opt.text(this.name).val(this.id);
        });
        layui.form.render();
    });
</script>
<script>
    var table,form,layer,laytpl;
    layui.use(['table','form','formSelects','laytpl','layer'], function(){
        table = layui.table;
        form=layui.form;
        layer=layui.layer;
        laytpl=layui.laytpl;
        //第一个实例
        table.render({
            elem: '#demo2'
            ,url: new Local().base_url+'/fenqv/notfen' //数据接口
            ,even : true
            ,id : "demo2"
            ,cols: [[ //表头
                {field:'id',type:'checkbox'}
                ,{field: 'id', title: 'ID'}
                ,{field: 'guanjian', title: '关键字'}
                ,{field:'weizhi',title:'位置'}
            ]]
        });
    });
</script>
<script>
    var i=0;
    var t=setInterval(function(){
        i++;
        var id=$("#id").val();
        if(id!=null&&id!=''){
            table.reload("demo2", {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where:{
                    id:id
                },done: function(res, page, count){
                    //可以自行添加判断的条件是否选中
                    //这句才是真正选中，通过设置关键字LAY_CHECKED为true选中，这里只对第一行选中
                    var zhi= $("#ids").val();
                    console.log(res.data);
                    var index;
                    for(var i=0;i<res.data.length;i++){
                        var dat=res.data;
                        if(dat[i].id==zhi){
                            res.data[i]["LAY_CHECKED"]='true';
                            index= res.data[i]['LAY_TABLE_INDEX'];
                        }
                    }
                    //下面三句是通过更改css来实现选中的效果
                    $('div[lay-id="demo2"] tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                    $('div[lay-id="demo2"] tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                }
            });
            clearInterval(t);
        }
        if(i>=4){
            clearInterval(t);
        }
    },200);
</script>